
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>[文件读写] · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part5.html" />
    
    
    <link rel="prev" href="part3.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter0/introduction.html">
            
                <a href="../chapter0/introduction.html">
            
                    
                    第零章：实验环境说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter1/introduction.html">
            
                <a href="../chapter1/introduction.html">
            
                    
                    第一章：独立式可执行程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter1/part1.html">
            
                <a href="../chapter1/part1.html">
            
                    
                    [安装 nightly rust]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter1/part2.html">
            
                <a href="../chapter1/part2.html">
            
                    
                    使用包管理器 cargo 创建 rust binary 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter1/part3.html">
            
                <a href="../chapter1/part3.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter1/part4.html">
            
                <a href="../chapter1/part4.html">
            
                    
                    [移除 runtime 依赖]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter1/part5.html">
            
                <a href="../chapter1/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter2/introduction.html">
            
                <a href="../chapter2/introduction.html">
            
                    
                    第二章：最小化内核
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter2/part1.html">
            
                <a href="../chapter2/part1.html">
            
                    
                    使用目标三元组描述目标平台
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter2/part2.html">
            
                <a href="../chapter2/part2.html">
            
                    
                    编译、生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter2/part3.html">
            
                <a href="../chapter2/part3.html">
            
                    
                    使用链接脚本指定内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter2/part4.html">
            
                <a href="../chapter2/part4.html">
            
                    
                    [重写程序入口点 -start]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter2/part5.html">
            
                <a href="../chapter2/part5.html">
            
                    
                    [使用 Qemu 运行内核]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter2/part6.html">
            
                <a href="../chapter2/part6.html">
            
                    
                    封装 SBI 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter2/part7.html">
            
                <a href="../chapter2/part7.html">
            
                    
                    [实现格式化输出]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter2/part8.html">
            
                <a href="../chapter2/part8.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter3/introduction.html">
            
                <a href="../chapter3/introduction.html">
            
                    
                    第三章：中断
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter3/part1.html">
            
                <a href="../chapter3/part1.html">
            
                    
                    rv64 中断介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter3/part2.html">
            
                <a href="../chapter3/part2.html">
            
                    
                    [手动触发断点中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter3/part3.html">
            
                <a href="../chapter3/part3.html">
            
                    
                    程序运行上下文环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter3/part4.html">
            
                <a href="../chapter3/part4.html">
            
                    
                    [实现上下文环境保存与恢复]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter3/part5.html">
            
                <a href="../chapter3/part5.html">
            
                    
                    [时钟中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter3/part6.html">
            
                <a href="../chapter3/part6.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter4/introduction.html">
            
                <a href="../chapter4/introduction.html">
            
                    
                    第四章：内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter4/part1.html">
            
                <a href="../chapter4/part1.html">
            
                    
                    [物理内存探测与管理]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter4/part2.html">
            
                <a href="../chapter4/part2.html">
            
                    
                    [动态内存分配]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter4/part3.html">
            
                <a href="../chapter4/part3.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter5/introduction.html">
            
                <a href="../chapter5/introduction.html">
            
                    
                    第五章：内存虚拟化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/part1.html">
            
                <a href="../chapter5/part1.html">
            
                    
                    页表：从虚拟内存到物理内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter5/part2.html">
            
                <a href="../chapter5/part2.html">
            
                    
                    [内核初始映射]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter5/part3.html">
            
                <a href="../chapter5/part3.html">
            
                    
                    内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter5/part4.html">
            
                <a href="../chapter5/part4.html">
            
                    
                    内核重映射实现之一：页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter5/part5.html">
            
                <a href="../chapter5/part5.html">
            
                    
                    内核重映射实现之二：MemorySet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter5/part6.html">
            
                <a href="../chapter5/part6.html">
            
                    
                    [内核重映射实现之三：完结]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter5/part7.html">
            
                <a href="../chapter5/part7.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter6/introduction.html">
            
                <a href="../chapter6/introduction.html">
            
                    
                    第六章：内核线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter6/part1.html">
            
                <a href="../chapter6/part1.html">
            
                    
                    线程状态与保存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter6/part2.html">
            
                <a href="../chapter6/part2.html">
            
                    
                    线程切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter6/part3.html">
            
                <a href="../chapter6/part3.html">
            
                    
                    内核线程初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter6/part4.html">
            
                <a href="../chapter6/part4.html">
            
                    
                    [内核线程创建与切换测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter6/part5.html">
            
                <a href="../chapter6/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter7/introduction.html">
            
                <a href="../chapter7/introduction.html">
            
                    
                    第七章：线程调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter7/part1.html">
            
                <a href="../chapter7/part1.html">
            
                    
                    线程池与线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter7/part2.html">
            
                <a href="../chapter7/part2.html">
            
                    
                    内核调度线程 idle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter7/part3.html">
            
                <a href="../chapter7/part3.html">
            
                    
                    线程调度之 Round Robin 算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter7/part4.html">
            
                <a href="../chapter7/part4.html">
            
                    
                    [线程调度测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter7/part5.html">
            
                <a href="../chapter7/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter8/introduction.html">
            
                <a href="../chapter8/introduction.html">
            
                    
                    第八章：进程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter8/part1.html">
            
                <a href="../chapter8/part1.html">
            
                    
                    [编写用户程序]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter8/part1_1.html">
            
                <a href="../chapter8/part1_1.html">
            
                    
                    合并内核与应用程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter8/part2.html">
            
                <a href="../chapter8/part2.html">
            
                    
                    在内核中实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter8/part3.html">
            
                <a href="../chapter8/part3.html">
            
                    
                    创建虚拟内存空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter8/part4.html">
            
                <a href="../chapter8/part4.html">
            
                    
                    [创建进程]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter8/part5.html">
            
                <a href="../chapter8/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="introduction.html">
            
                <a href="introduction.html">
            
                    
                    第九章：文件系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    [使用文件系统]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    [实现记事本]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    [实现终端]
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.11.4" data-path="part4.html">
            
                <a href="part4.html">
            
                    
                    [文件读写]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="part5.html">
            
                <a href="part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter10/introduction.html">
            
                <a href="../chapter10/introduction.html">
            
                    
                    第十章：同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../exercise/introduction.html">
            
                <a href="../exercise/introduction.html">
            
                    
                    练习题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../exercise/part1.html">
            
                <a href="../exercise/part1.html">
            
                    
                    1. 中断异常
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../exercise/part2.html">
            
                <a href="../exercise/part2.html">
            
                    
                    2. 物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../exercise/part3.html">
            
                <a href="../exercise/part3.html">
            
                    
                    3. 虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../exercise/part4.html">
            
                <a href="../exercise/part4.html">
            
                    
                    4. 线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.5" data-path="../exercise/part5.html">
            
                <a href="../exercise/part5.html">
            
                    
                    5. 用户进程（+ 虚拟内存管理 + 线程管理）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.6" data-path="../exercise/part6.html">
            
                <a href="../exercise/part6.html">
            
                    
                    6. CPU 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.7" data-path="../exercise/part7.html">
            
                <a href="../exercise/part7.html">
            
                    
                    7. 同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.8" data-path="../exercise/part8.html">
            
                <a href="../exercise/part8.html">
            
                    
                    8. 文件系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../appendix/introduction.html">
            
                <a href="../appendix/introduction.html">
            
                    
                    附录
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../appendix/inline_asm.html">
            
                <a href="../appendix/inline_asm.html">
            
                    
                    内联汇编
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../appendix/install_rust.html">
            
                <a href="../appendix/install_rust.html">
            
                    
                    安装 rust
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >[文件读写]</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6587;&#x4EF6;&#x8BFB;&#x5199;">&#x6587;&#x4EF6;&#x8BFB;&#x5199;</h2>
<ul>
<li><a href="https://github.com/rcore-os/rCore_tutorial/tree/ch9-pa4" target="_blank">&#x4EE3;&#x7801;</a></li>
</ul>
<blockquote>
<p>&#x5FC3;&#x6001;&#x70B8;&#x4E86;&#x3002;&#x672C;&#x6765;&#x5DF2;&#x7ECF;&#x5199;&#x597D;&#x4E86;&#xFF0C;&#x56E0;&#x4E3A;&#x6587;&#x4EF6;&#x4E0D;&#x5C0F;&#x5FC3;&#x88AB;&#x5220;&#x4E86;&#x53EA;&#x80FD;&#x91CD;&#x5199;&#x4E00;&#x904D;&#x3002;:cry:</p>
</blockquote>
<p>&#x6211;&#x4EEC;&#x8981;&#x5728;&#x7528;&#x6237;&#x6001;&#x652F;&#x6301;&#x6587;&#x4EF6;&#x8BFB;&#x5199;&#x529F;&#x80FD;&#x3002;&#x5177;&#x4F53;&#x7684;&#x7528;&#x6237;&#x6001;&#x7A0B;&#x5E8F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/bin/write.rs</span>

<span class="token attribute attr-name">#![no_std]</span>
<span class="token attribute attr-name">#![no_main]</span>

<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">alloc</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[macro_use]</span>
<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">user</span><span class="token punctuation">;</span>

<span class="token keyword">use</span> <span class="token namespace">user<span class="token punctuation">::</span>io<span class="token punctuation">::</span></span><span class="token operator">*</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">user<span class="token punctuation">::</span>syscall<span class="token punctuation">::</span></span><span class="token punctuation">{</span>
    sys_open<span class="token punctuation">,</span>
    sys_close<span class="token punctuation">,</span>
    sys_read<span class="token punctuation">,</span>
    sys_write<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> <span class="token constant">BUFFER_SIZE</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">FILE</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token lifetime-annotation symbol">&apos;static</span> <span class="token keyword">str</span> <span class="token operator">=</span> <span class="token string">&quot;temp\0&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token constant">TEXT</span><span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token lifetime-annotation symbol">&apos;static</span> <span class="token keyword">str</span> <span class="token operator">=</span> <span class="token string">&quot;Hello world!\0&quot;</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">usize</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x5199;&#x5230;&#x6587;&#x4EF6; temp &#x4E2D;</span>
    <span class="token keyword">let</span> write_fd <span class="token operator">=</span> <span class="token function">sys_open</span><span class="token punctuation">(</span><span class="token constant">FILE</span><span class="token punctuation">.</span><span class="token function">as_ptr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">O_WRONLY</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sys_write</span><span class="token punctuation">(</span>write_fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> <span class="token constant">TEXT</span><span class="token punctuation">.</span><span class="token function">as_ptr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">TEXT</span><span class="token punctuation">.</span><span class="token function">len</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;write to file &apos;temp&apos; successfully...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sys_close</span><span class="token punctuation">(</span>write_fd <span class="token keyword">as</span> <span class="token keyword">i32</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x4ECE;&#x6587;&#x4EF6; temp &#x8BFB;&#x5165;&#x5185;&#x5B58;</span>
    <span class="token keyword">let</span> read_fd <span class="token operator">=</span> <span class="token function">sys_open</span><span class="token punctuation">(</span><span class="token constant">FILE</span><span class="token punctuation">.</span><span class="token function">as_ptr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">O_RDONLY</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> <span class="token keyword">mut</span> read <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0u8</span><span class="token punctuation">;</span> <span class="token constant">BUFFER_SIZE</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token function">sys_read</span><span class="token punctuation">(</span>read_fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>read<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> <span class="token constant">BUFFER_SIZE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;read from file &apos;temp&apos; successfully...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x68C0;&#x67E5;&#x529F;&#x80FD;&#x662F;&#x5426;&#x6B63;&#x786E;</span>
    <span class="token keyword">let</span> len <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">..</span><span class="token constant">BUFFER_SIZE</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token closure-params"><span class="token closure-punctuation punctuation">|</span><span class="token operator">&amp;</span>i<span class="token closure-punctuation punctuation">|</span></span> read<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token keyword">u8</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">print!</span><span class="token punctuation">(</span><span class="token string">&quot;content = &quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token number">0usize</span><span class="token punctuation">..</span>len <span class="token punctuation">{</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span>read<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token constant">TEXT</span><span class="token punctuation">.</span><span class="token function">as_bytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">putchar</span><span class="token punctuation">(</span>read<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">putchar</span><span class="token punctuation">(</span><span class="token char string">&apos;\n&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sys_close</span><span class="token punctuation">(</span>read_fd <span class="token keyword">as</span> <span class="token keyword">i32</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token number">0</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x8981;&#x5B9E;&#x73B0;&#x4E24;&#x4E2A;&#x65B0;&#x7684;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/syscall.rs</span>

<span class="token keyword">enum</span> <span class="token type-definition class-name">SyscallId</span> <span class="token punctuation">{</span>
    <span class="token class-name">Open</span> <span class="token operator">=</span> <span class="token number">56</span><span class="token punctuation">,</span>
    <span class="token class-name">Close</span> <span class="token operator">=</span> <span class="token number">57</span><span class="token punctuation">,</span>
    <span class="token punctuation">...</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_open</span><span class="token punctuation">(</span>path<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> flags<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i64</span> <span class="token punctuation">{</span>
    <span class="token function">sys_call</span><span class="token punctuation">(</span><span class="token class-name">SyscallId</span><span class="token punctuation">::</span><span class="token class-name">Open</span><span class="token punctuation">,</span> path <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> flags <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_close</span><span class="token punctuation">(</span>fd<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i64</span> <span class="token punctuation">{</span>
    <span class="token function">sys_call</span><span class="token punctuation">(</span><span class="token class-name">SyscallId</span><span class="token punctuation">::</span><span class="token class-name">Close</span><span class="token punctuation">,</span> fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p><code>sys_open, sys_close</code> &#x7684; syscall id &#x5206;&#x522B;&#x4E3A; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>5</mn><mn>6</mn><mo separator="true">,</mo><mn>5</mn><mn>7</mn></mrow><annotation encoding="application/x-tex">56, 57</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">5</span><span class="mord mathrm">6</span><span class="mpunct">,</span><span class="mord mathrm">5</span><span class="mord mathrm">7</span></span></span></span> &#x3002;&#x4E3A;&#x4E86;&#x8BF4;&#x660E;&#x5B83;&#x4EEC;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5148;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26; (fd, File Descriptor) &#x7684;&#x6982;&#x5FF5;&#x3002;&#x5728;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#x5185;&#xFF0C;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x7528;&#x6765;&#x63CF;&#x8FF0;&#x4E00;&#x4E2A;&#x6253;&#x5F00;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x5B83;&#x88AB;&#x6240;&#x6709;&#x7684;&#x7EBF;&#x7A0B;&#x5171;&#x4EAB;&#x3002;&#x800C; <code>sys_open</code> &#x7684;&#x529F;&#x80FD;&#x662F;&#x4E3A;&#x5F53;&#x524D;&#x8FDB;&#x7A0B;&#x6253;&#x5F00;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x5BF9;&#x5E94;&#x7684;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x3002;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x63A5;&#x4E0B;&#x6765;&#x53EF;&#x88AB;&#x8BFB;&#x5199;&#x76F8;&#x5173;&#x7684;&#x7CFB;&#x7EDF;&#x8C03;&#x7528; <code>sys_read, sys_write</code> &#x5229;&#x7528;&#x6765;&#x8BBF;&#x95EE;&#x5BF9;&#x5E94;&#x7684;&#x6587;&#x4EF6;&#x3002;&#x5F53;&#x6240;&#x6709;&#x7684;&#x8BBF;&#x95EE;&#x7ED3;&#x675F;&#x540E;&#xFF0C;&#x8FDB;&#x7A0B;&#x901A;&#x8FC7; <code>sys_close</code> &#x5173;&#x95ED;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x5BF9;&#x5E94;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x56DE;&#x6536;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x770B;&#x5230;&#xFF0C;&#x4F20;&#x5165; <code>sys_open</code> &#x7684;&#x53C2;&#x6570;&#x9664;&#x4E86; <code>path</code> &#x8868;&#x793A;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x6807;&#x5FD7;&#x4F4D; <code>flags</code>&#x3002;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x662F;&#x63A7;&#x5236;&#x5F53;&#x524D;&#x8FDB;&#x7A0B;&#x5BF9;&#x6253;&#x5F00;&#x6587;&#x4EF6;&#x7684;&#x8BFB;&#x5199;&#x6743;&#x9650;&#x3002;&#x4E0E; Linux &#x4E00;&#x81F4;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E0B;&#x9762;&#x7684;&#x6807;&#x5FD7;&#x4F4D;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/io.rs</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">O_RDONLY</span><span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>    <span class="token comment">// &#x53EA;&#x8BFB;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">O_WRONLY</span><span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>    <span class="token comment">// &#x53EA;&#x5199;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">O_RDWR</span><span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span>        <span class="token comment">// &#x53EF;&#x8BFB;&#x53EF;&#x5199;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">O_CREAT</span><span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token number">64</span><span class="token punctuation">;</span>    <span class="token comment">// &#x6253;&#x5F00;&#x6587;&#x4EF6;&#x65F6;&#x82E5;&#x6587;&#x4EF6;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x521B;&#x5EFA;&#x5B83;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">O_APPEND</span><span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token number">1024</span><span class="token punctuation">;</span>    <span class="token comment">// &#x4ECE;&#x6587;&#x4EF6;&#x7ED3;&#x5C3E;&#x5F00;&#x59CB;&#x5199;&#x5165;</span>
</code></pre>
<p>&#x6B64;&#x5916;&#xFF0C;&#x4E4B;&#x524D;&#x5B9A;&#x4E49;&#x7684; <code>sys_write</code> &#x529F;&#x80FD;&#x592A;&#x5F31;&#xFF0C;&#x4EC5;&#x652F;&#x6301;&#x5411;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x5199;&#x5165;&#x5355;&#x4E2A;&#x5B57;&#x7B26;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5176;&#x6309;&#x7167; <code>sys_read</code> &#x7684;&#x6807;&#x51C6;&#x91CD;&#x5199;&#xFF0C;&#x652F;&#x6301;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x3001;&#x5185;&#x5B58;&#x7F13;&#x51B2;&#x533A;&#x4EE5;&#x53CA;&#x957F;&#x5EA6;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/syscall.rs</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_write</span><span class="token punctuation">(</span>fd<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> base<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> len<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i64</span> <span class="token punctuation">{</span>
    <span class="token function">sys_call</span><span class="token punctuation">(</span><span class="token class-name">SyscallId</span><span class="token punctuation">::</span><span class="token class-name">Write</span><span class="token punctuation">,</span> fd<span class="token punctuation">,</span> base <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> len<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8C03;&#x7528; <code>sys_write</code> &#x7684;&#x5730;&#x65B9;&#x4E5F;&#x8981;&#x505A;&#x76F8;&#x5E94;&#x4FEE;&#x6539;&#xFF1A;</p>
<pre class="language-"><code class="lang-diff">// usr/rust/src/io.rs
<span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">pub const STDOUT: usize = 1;
</span></span>pub fn putchar(ch: char) {
<span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line">   sys_write(ch as u8);
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">   sys_write(STDOUT, &amp;ch as *const char as *const u8, 1);
</span></span>}
</code></pre>
<p>&#x6CE8;&#x610F;&#x5230;&#x6211;&#x4EEC;&#x5728;&#x6253;&#x5F00;&#x6587;&#x4EF6;&#x65F6;&#x5E76;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x6807;&#x5FD7;&#x4F4D; <code>O_CREAT</code> &#xFF0C;&#x56E0;&#x6B64;&#x5047;&#x5B9A;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x5185; <code>temp</code> &#x6587;&#x4EF6;&#x4E00;&#x5B9A;&#x5B58;&#x5728;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x76F4;&#x63A5;&#x628A;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x548C;&#x4F17;&#x591A;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x4E00;&#x8D77;&#x6253;&#x5305;&#x5230;&#x78C1;&#x76D8;&#x955C;&#x50CF;&#x4E2D;&#x3002;</p>
<pre class="language-"><code class="lang-diff">//  usr/Makefile
$(sfsimg): rcore-fs-fuse rust
<span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">   @dd if=/dev/zero of=$(out_dir)/temp bs=1k count=2
</span></span><span class="token unchanged"><span class="token prefix unchanged"> </span><span class="token line">   @rcore-fs-fuse --fs sfs $@ $(out_dir) zip
</span></span></code></pre>
<p>&#x73B0;&#x5728;&#x7528;&#x6237;&#x6001;&#x51C6;&#x5907;&#x5B8C;&#x6BD5;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x770B;&#x5185;&#x6838;&#x6001;&#x8981;&#x505A;&#x51FA;&#x54EA;&#x4E9B;&#x53D8;&#x5316;&#x3002;</p>
<p>&#x9996;&#x5148;&#x6211;&#x4EEC;&#x6765;&#x652F;&#x6301;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x673A;&#x5236;&#x3002;&#x5B83;&#x662F;&#x7528;&#x6765;&#x63CF;&#x8FF0;&#x8FDB;&#x7A0B;&#x5185;&#x6253;&#x5F00;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x800C;&#x8FD9;&#x4E2A;&#x7ED3;&#x6784;&#x4F53;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/fs/mod.rs</span>
<span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">file</span><span class="token punctuation">;</span>

<span class="token comment">// os/src/fs/file.rs</span>
<span class="token keyword">use</span> <span class="token namespace">alloc<span class="token punctuation">::</span>sync<span class="token punctuation">::</span></span><span class="token class-name">Arc</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">rcore_fs<span class="token punctuation">::</span>vfs<span class="token punctuation">::</span></span><span class="token class-name">INode</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">rcore_fs_sfs<span class="token punctuation">::</span></span><span class="token class-name">INodeImpl</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>fs<span class="token punctuation">::</span></span><span class="token constant">ROOT_INODE</span><span class="token punctuation">;</span>

<span class="token comment">// &#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x7C7B;&#x578B;</span>
<span class="token attribute attr-name">#[derive(Copy,Clone,Debug)]</span>
<span class="token keyword">pub</span> <span class="token keyword">enum</span> <span class="token type-definition class-name">FileDescriptorType</span> <span class="token punctuation">{</span>
    <span class="token constant">FD_NONE</span><span class="token punctuation">,</span>    <span class="token comment">// &#x7A7A;&#x7C7B;&#x578B;</span>
    <span class="token constant">FD_INODE</span><span class="token punctuation">,</span>    <span class="token comment">// INode &#x7C7B;&#x578B;</span>
    <span class="token constant">FD_DEVICE</span><span class="token punctuation">,</span>    <span class="token comment">// &#x8BBE;&#x5907;&#x7C7B;&#x578B;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x8FDB;&#x7A0B;&#x5185;&#x6253;&#x5F00;&#x7684;&#x6587;&#x4EF6;</span>
<span class="token attribute attr-name">#[derive(Clone)]</span>
<span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">File</span> <span class="token punctuation">{</span>
    fdtype<span class="token punctuation">:</span> <span class="token class-name">FileDescriptorType</span><span class="token punctuation">,</span>
    <span class="token comment">// &#x8FDB;&#x7A0B;&#x5BF9;&#x8BE5;&#x6587;&#x4EF6;&#x7684;&#x6743;&#x9650;</span>
    readable<span class="token punctuation">:</span> <span class="token keyword">bool</span><span class="token punctuation">,</span>
    writable<span class="token punctuation">:</span> <span class="token keyword">bool</span><span class="token punctuation">,</span>
    <span class="token comment">// &#x8BE5;&#x6587;&#x4EF6;&#x7684; INode &#x6307;&#x9488;&#xFF0C;&#x7528;&#x4E8E;&#x8FDB;&#x884C;&#x5B9E;&#x9645;&#x8BFB;&#x5199;</span>
    <span class="token keyword">pub</span> inode<span class="token punctuation">:</span> <span class="token class-name">Option</span><span class="token operator">&lt;</span><span class="token class-name">Arc</span><span class="token operator">&lt;</span><span class="token keyword">dyn</span> <span class="token class-name">INode</span><span class="token operator">&gt;&gt;</span><span class="token punctuation">,</span>
    <span class="token comment">// &#x8FDB;&#x7A0B;&#x4E2D;&#x8BE5;&#x6587;&#x4EF6;&#x7684;&#x504F;&#x79FB;&#x91CF;&#x6307;&#x9488;</span>
    offset<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E00;&#x4E9B;&#x7B80;&#x5355;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/fs/file.rs</span>
<span class="token keyword">impl</span> <span class="token class-name">File</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x521D;&#x59CB;&#x5316;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">Self</span> <span class="token punctuation">{</span>
        <span class="token class-name">File</span> <span class="token punctuation">{</span>
            fdtype<span class="token punctuation">:</span> <span class="token class-name">FileDescriptorType</span><span class="token punctuation">::</span><span class="token constant">FD_NONE</span><span class="token punctuation">,</span>
            readable<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
            writable<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
            inode<span class="token punctuation">:</span> <span class="token class-name">None</span><span class="token punctuation">,</span>
            offset<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// get/set &#x51FD;&#x6570;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">set_readable</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> v<span class="token punctuation">:</span> <span class="token keyword">bool</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>readable <span class="token operator">=</span> v<span class="token punctuation">;</span> <span class="token punctuation">}</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">set_writable</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> v<span class="token punctuation">:</span> <span class="token keyword">bool</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>writable <span class="token operator">=</span> v<span class="token punctuation">;</span> <span class="token punctuation">}</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">get_readable</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">bool</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>readable <span class="token punctuation">}</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">get_writable</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">bool</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>writable <span class="token punctuation">}</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">set_fdtype</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> t<span class="token punctuation">:</span> <span class="token class-name">FileDescriptorType</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>fdtype <span class="token operator">=</span> t<span class="token punctuation">;</span> <span class="token punctuation">}</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">get_fdtype</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">FileDescriptorType</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>fdtype <span class="token punctuation">}</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">set_offset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> o<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>offset <span class="token operator">=</span> o<span class="token punctuation">;</span> <span class="token punctuation">}</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">get_offset</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">usize</span> <span class="token punctuation">{</span> <span class="token keyword">self</span><span class="token punctuation">.</span>offset <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6BCF;&#x4E2A;&#x8FDB;&#x7A0B;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x6253;&#x5F00;&#x7684;&#x6587;&#x4EF6;&#x5217;&#x8868;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x8FDB;&#x7A0B;&#x63A7;&#x5236;&#x5757;&#x4E2D;&#x5F00;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B57;&#x6BB5; <code>ofile</code> &#x3002;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x4E00;&#x9879;&#x5982;&#x679C;&#x662F; <code>None</code> &#xFF0C;&#x8868;&#x660E;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x53EF;&#x7528;&#x3002;</p>
<pre class="language-"><code class="lang-diff">// os/src/consts.rs
<span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">pub const NOFILE: usize = 16;
</span></span>
// os/src/process/structs.rs
<span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">use crate::fs::file::File;
</span><span class="token prefix inserted">+</span><span class="token line">use spin::Mutex;
</span><span class="token prefix inserted">+</span><span class="token line">use alloc::sync::Arc;
</span></span>pub struct Thread {
<span class="token unchanged"><span class="token prefix unchanged"> </span><span class="token line">   pub context: Context,
</span><span class="token prefix unchanged"> </span><span class="token line">   pub kstack: KernelStack,
</span><span class="token prefix unchanged"> </span><span class="token line">   pub wait: Option&lt;Tid&gt;,
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">   pub ofile: [Option&lt;Arc&lt;Mutex&lt;File&gt;&gt;&gt;; NOFILE],
</span></span>}
</code></pre>
<p>&#x5728;&#x65B0;&#x5EFA;&#x5185;&#x6838;&#x7EBF;&#x7A0B; <code>new_kernel</code> &#x4EE5;&#x53CA;&#x83B7;&#x5F97;&#x542F;&#x52A8;&#x7EBF;&#x7A0B; <code>get_boot_thread</code> &#x4E2D;&#xFF0C;&#x6700;&#x7EC8;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4E5F;&#x9700;&#x8981;&#x8FDB;&#x884C;&#x53D8;&#x5316;&#xFF1A;</p>
<pre class="language-"><code class="lang-diff">// os/src/process/structs.rs
Box::new(Thread {
<span class="token unchanged"><span class="token prefix unchanged"> </span><span class="token line">   ...
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">   ofile: [None; NOFILE],
</span></span>})
</code></pre>
<p>&#x4E3A;&#x4E86;&#x80FD;&#x591F;&#x7F16;&#x8BD1;&#x901A;&#x8FC7;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6253;&#x5F00;&#x4EE5;&#x4E0B;&#x5F00;&#x5173;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/lib.rs</span>
<span class="token attribute attr-name">#![feature(const_in_array_repeat_expressions)]</span>
</code></pre>
<p>&#x65B0;&#x5EFA;&#x7528;&#x6237;&#x8FDB;&#x7A0B; <code>new_user</code> &#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x8FDB;&#x884C;&#x7C7B;&#x4F3C;&#x7684;&#x521D;&#x59CB;&#x5316;&#xFF0C;&#x4F46;&#x662F;&#x8981;&#x5C06;&#x6570;&#x7EC4;&#x7684;&#x524D;&#x4E09;&#x9879; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>0</mn><mo separator="true">,</mo><mn>1</mn><mo separator="true">,</mo><mn>2</mn></mrow><annotation encoding="application/x-tex">0,1,2</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.8388800000000001em;vertical-align:-0.19444em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">0</span><span class="mpunct">,</span><span class="mord mathrm">1</span><span class="mpunct">,</span><span class="mord mathrm">2</span></span></span></span> &#x8D4B;&#x503C;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x5206;&#x522B;&#x8868;&#x793A;&#x6807;&#x51C6;&#x8F93;&#x5165; (stdin)&#x3001;&#x6807;&#x51C6;&#x8F93;&#x51FA; (stdout)&#x3001;&#x6807;&#x51C6;&#x9519;&#x8BEF;&#x8F93;&#x51FA; (stderr) &#xFF0C;&#x5728;&#x521B;&#x5EFA;&#x7528;&#x6237;&#x8FDB;&#x7A0B;&#x7684;&#x65F6;&#x5019;&#x9ED8;&#x8BA4;&#x6253;&#x5F00;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x518D;&#x5206;&#x914D;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x662F;&#x4ECE; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mn>3</mn></mrow><annotation encoding="application/x-tex">3</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.64444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">3</span></span></span></span> &#x5F00;&#x59CB;&#x5206;&#x914D;&#x7684;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">//  os/src/process/structs.rs</span>
<span class="token keyword">impl</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">unsafe</span> <span class="token keyword">fn</span> <span class="token function-definition function">new_user</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token punctuation">[</span><span class="token keyword">u8</span><span class="token punctuation">]</span><span class="token punctuation">,</span> wait_thread<span class="token punctuation">:</span> <span class="token class-name">Option</span><span class="token operator">&lt;</span><span class="token class-name">Tid</span><span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Box</span><span class="token operator">&lt;</span><span class="token class-name">Thread</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
        <span class="token punctuation">...</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> thread <span class="token operator">=</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
            context<span class="token punctuation">:</span> <span class="token class-name">Context</span><span class="token punctuation">::</span><span class="token function">new_user_thread</span><span class="token punctuation">(</span>entry_addr<span class="token punctuation">,</span> ustack_top<span class="token punctuation">,</span> kstack<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> vm<span class="token punctuation">.</span><span class="token function">token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            kstack<span class="token punctuation">:</span> kstack<span class="token punctuation">,</span>
            wait<span class="token punctuation">:</span> wait_thread<span class="token punctuation">,</span>
            ofile<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token class-name">None</span><span class="token punctuation">;</span> <span class="token constant">NOFILE</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">..</span><span class="token number">3</span> <span class="token punctuation">{</span>
            thread<span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Arc</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">Mutex</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token class-name">Box</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span>thread<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5728;&#x8FDB;&#x7A0B;&#x4E2D;&#x5206;&#x914D;&#x3001;&#x56DE;&#x6536;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">//  os/src/process/structs.rs</span>
<span class="token keyword">impl</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5206;&#x914D;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">alloc_fd</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i32</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> fd <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token number">0usize</span><span class="token punctuation">..</span><span class="token constant">NOFILE</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token keyword">self</span><span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">is_none</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                fd <span class="token operator">=</span> i<span class="token punctuation">;</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token class-name">Arc</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">Mutex</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        fd <span class="token keyword">as</span> <span class="token keyword">i32</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x56DE;&#x6536;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">dealloc_fd</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> fd<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">is_some</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">None</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x5728;&#x5185;&#x6838;&#x6001;&#x4E2D;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x8FD9;&#x4E9B;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/syscall.rs</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">SYS_OPEN</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">56</span><span class="token punctuation">;</span>
<span class="token keyword">pub</span> <span class="token keyword">const</span> <span class="token constant">SYS_CLOSE</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">57</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">syscall</span><span class="token punctuation">(</span>id<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> args<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">usize</span><span class="token punctuation">;</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> tf<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token class-name">TrapFrame</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">match</span> id <span class="token punctuation">{</span>
        <span class="token constant">SYS_OPEN</span> <span class="token operator">=&gt;</span> <span class="token function">sys_open</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token keyword">i32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token constant">SYS_CLOSE</span> <span class="token operator">=&gt;</span> <span class="token function">sys_close</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token keyword">i32</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token constant">SYS_READ</span> <span class="token operator">=&gt;</span> <span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token function">sys_read</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token constant">SYS_WRITE</span> <span class="token operator">=&gt;</span> <span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token function">sys_write</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">...</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0E;&#x4E4B;&#x524D;&#x7684;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x4E0D;&#x540C;&#x4E4B;&#x5904;&#x5728;&#x4E8E;&#xFF0C;&#x5728;&#x8FD9;&#x4E9B;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x5185;&#xFF0C;&#x5F53;&#x524D;&#x8FDB;&#x7A0B;&#x7684;&#x8FDB;&#x7A0B;&#x63A7;&#x5236;&#x5757;&#x53EF;&#x80FD;&#x88AB;&#x4FEE;&#x6539;&#x3002;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5F53;&#x524D;&#x8FDB;&#x7A0B;&#x8FDB;&#x7A0B;&#x63A7;&#x5236;&#x5757;&#x7684;&#x53EF;&#x53D8;&#x5F15;&#x7528;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/process/processor.rs</span>
<span class="token keyword">impl</span> <span class="token class-name">Processor</span> <span class="token punctuation">{</span>
    <span class="token punctuation">...</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">current_thread_mut</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token number">1</span><span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// os/src/process/mod.rs</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">current_thread_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">&amp;</span><span class="token lifetime-annotation symbol">&apos;static</span> <span class="token keyword">mut</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">current_thread_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4F9D;&#x6B21;&#x5B9E;&#x73B0;&#x8FD9;&#x4E9B;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x5BF9;&#x4E8E; <code>sys_open</code> &#xFF0C;&#x5206;&#x914D;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#xFF0C;&#x5E76;&#x627E;&#x5230;&#x5BF9;&#x5E94;&#x6587;&#x4EF6;&#x7684; INode &#xFF0C;&#x5C06;&#x6307;&#x9488;&#x4FDD;&#x5B58;&#x4E0B;&#x6765;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/syscall.rs</span>
<span class="token keyword">fn</span> <span class="token function-definition function">sys_open</span><span class="token punctuation">(</span>path<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> flags<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> thread <span class="token operator">=</span> <span class="token namespace">process<span class="token punctuation">::</span></span><span class="token function">current_thread_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> fd <span class="token operator">=</span> thread<span class="token punctuation">.</span><span class="token function">alloc_fd</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">isize</span><span class="token punctuation">;</span>
    thread<span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">]</span>
        <span class="token punctuation">.</span><span class="token function">as_ref</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">open_file</span><span class="token punctuation">(</span><span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token function">from_cstr</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> flags<span class="token punctuation">)</span><span class="token punctuation">;</span>
    fd
<span class="token punctuation">}</span>
<span class="token comment">// os/src/fs/file.rs</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">open_file</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> path<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token lifetime-annotation symbol">&apos;static</span> <span class="token keyword">str</span><span class="token punctuation">,</span> flags<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_fdtype</span><span class="token punctuation">(</span><span class="token class-name">FileDescriptorType</span><span class="token punctuation">::</span><span class="token constant">FD_INODE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_readable</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>flags <span class="token operator">&amp;</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_readable</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>flags <span class="token operator">&amp;</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_writable</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>inode <span class="token operator">=</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token constant">ROOT_INODE</span><span class="token punctuation">.</span><span class="token function">lookup</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">set_offset</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5BF9;&#x4E8E; <code>sys_close</code> &#xFF0C;&#x76F4;&#x63A5;&#x56DE;&#x6536;&#x5BF9;&#x5E94;&#x7684;&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;&#x5373;&#x53EF;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/syscall.rs</span>
<span class="token keyword">fn</span> <span class="token function-definition function">sys_close</span><span class="token punctuation">(</span>fd<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> thread <span class="token operator">=</span> <span class="token namespace">process<span class="token punctuation">::</span></span><span class="token function">current_thread_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">assert!</span><span class="token punctuation">(</span>thread<span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">is_some</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    thread<span class="token punctuation">.</span><span class="token function">dealloc_fd</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token number">0</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5BF9;&#x4E8E; <code>sys_open</code> &#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#x6807;&#x51C6;&#x8F93;&#x5165;&#x7684;&#x8BDD;&#xFF0C;&#x5E94;&#x8BE5;&#x4ECE;&#x5BF9;&#x5E94;&#x7684; INode &#x4E2D;&#x8BFB;&#x5165;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/syscall.rs</span>
<span class="token keyword">unsafe</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_read</span><span class="token punctuation">(</span>fd<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> base<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> len<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> fd <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
        <span class="token comment">// &#x5982;&#x679C;&#x662F;&#x6807;&#x51C6;&#x8F93;&#x5165;</span>
        <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
            <span class="token operator">*</span>base <span class="token operator">=</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>fs<span class="token punctuation">::</span>stdio<span class="token punctuation">::</span></span><span class="token constant">STDIN</span><span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">u8</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> thread <span class="token operator">=</span> <span class="token namespace">process<span class="token punctuation">::</span></span><span class="token function">current_thread_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span>thread<span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">is_some</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> file <span class="token operator">=</span> thread<span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">as_ref</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">get_readable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">match</span> file<span class="token punctuation">.</span><span class="token function">get_fdtype</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">FileDescriptorType</span><span class="token punctuation">::</span><span class="token constant">FD_INODE</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">let</span> <span class="token keyword">mut</span> offset <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">get_offset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">let</span> s <span class="token operator">=</span> file
                    <span class="token punctuation">.</span>inode
                    <span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">read_at</span><span class="token punctuation">(</span>offset<span class="token punctuation">,</span> <span class="token namespace">core<span class="token punctuation">::</span>slice<span class="token punctuation">::</span></span><span class="token function">from_raw_parts_mut</span><span class="token punctuation">(</span>base<span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                offset <span class="token operator">+=</span> s<span class="token punctuation">;</span>
                file<span class="token punctuation">.</span><span class="token function">set_offset</span><span class="token punctuation">(</span>offset<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> s <span class="token keyword">as</span> <span class="token keyword">isize</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            _ <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;fdtype not handled!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5BF9;&#x4E8E; <code>sys_write</code> &#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x7684;&#x8BDD;&#xFF0C;&#x8F93;&#x5165;&#x5230;&#x5BF9;&#x5E94;&#x7684; INode &#x4E2D;&#x3002;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// os/src/syscall.rs</span>
<span class="token keyword">unsafe</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_write</span><span class="token punctuation">(</span>fd<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> base<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">,</span> len<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> fd <span class="token operator">==</span> <span class="token number">1</span> <span class="token punctuation">{</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span>len <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>io<span class="token punctuation">::</span></span><span class="token function">putchar</span><span class="token punctuation">(</span><span class="token operator">*</span>base <span class="token keyword">as</span> <span class="token keyword">char</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> thread <span class="token operator">=</span> <span class="token namespace">process<span class="token punctuation">::</span></span><span class="token function">current_thread_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span>thread<span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">is_some</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> file <span class="token operator">=</span> thread<span class="token punctuation">.</span>ofile<span class="token punctuation">[</span>fd <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">as_ref</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token macro property">assert!</span><span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">get_writable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">match</span> file<span class="token punctuation">.</span><span class="token function">get_fdtype</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">FileDescriptorType</span><span class="token punctuation">::</span><span class="token constant">FD_INODE</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token keyword">let</span> <span class="token keyword">mut</span> offset <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">get_offset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">let</span> s <span class="token operator">=</span> file
                    <span class="token punctuation">.</span>inode
                    <span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">write_at</span><span class="token punctuation">(</span>offset<span class="token punctuation">,</span> <span class="token namespace">core<span class="token punctuation">::</span>slice<span class="token punctuation">::</span></span><span class="token function">from_raw_parts</span><span class="token punctuation">(</span>base<span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">)</span>
                    <span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                offset <span class="token operator">+=</span> s<span class="token punctuation">;</span>
                file<span class="token punctuation">.</span><span class="token function">set_offset</span><span class="token punctuation">(</span>offset<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> s <span class="token keyword">as</span> <span class="token keyword">isize</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            _ <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;fdtype not handled!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token number">0</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x80FD;&#x5426;&#x6B63;&#x5E38;&#x8FD0;&#x884C;&#xFF1A;</p>
<blockquote>
<p><strong>[success] &#x6587;&#x4EF6;&#x8BFB;&#x5199;&#x6D4B;&#x8BD5;</strong></p>
<pre class="language-"><code class="lang-rust"><span class="token operator">&gt;&gt;</span> rust<span class="token operator">/</span>write
searching <span class="token keyword">for</span> program rust<span class="token operator">/</span>write
write to file <span class="token lifetime-annotation symbol">&apos;temp</span>&apos; successfully<span class="token punctuation">...</span>
read from file <span class="token lifetime-annotation symbol">&apos;temp</span>&apos; successfully<span class="token punctuation">...</span>
content <span class="token operator">=</span> <span class="token class-name">Hello</span> <span class="token macro property">world!</span>
thread <span class="token number">1</span> exited<span class="token punctuation">,</span> exit code <span class="token operator">=</span> <span class="token number">0</span>
</code></pre>
</blockquote>
<p>&#x6240;&#x6709;&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5728;<a href="https://github.com/rcore-os/rCore_tutorial/tree/ch9-pa4" target="_blank">&#x8FD9;&#x91CC;</a>&#x627E;&#x5230;&#x3002;</p>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css"></p>
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
  var gitalk = new Gitalk({
    clientID: "ab871063a9fa7f1da571",
    clientSecret: "4760b384e3e452d2241d859cfa6a7da21a8729ec",
    repo: "rCore_tutorial_doc",
    owner: "rcore-os",
    admin: [
      "chyyuu",
      "xyongcn",
      "wangrunji0408",
      "xy-plus",
      "PanQL",
      "wyfcyx",
      "equation314"
    ],
    id: location.pathname,
    distractionFreeMode: false
  });
  gitalk.render("gitalk-container");
</script>
</div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part3.html" class="navigation navigation-prev " aria-label="Previous page: [实现终端]">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part5.html" class="navigation navigation-next " aria-label="Next page: 总结与展望">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"[文件读写]","level":"1.11.4","depth":2,"next":{"title":"总结与展望","level":"1.11.5","depth":2,"path":"chapter9/part5.md","ref":"chapter9/part5.md","articles":[]},"previous":{"title":"[实现终端]","level":"1.11.3","depth":2,"path":"chapter9/part3.md","ref":"chapter9/part3.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","-highlight","prism","localized-footer","mermaid-gb3"],"pluginsConfig":{"chapter-fold":{},"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"emphasize":{},"search":{},"localized-footer":{"filename":"extensions/comment/gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"hide-element":{"elements":[".gitbook-link"]},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter9/part4.md","mtime":"2020-09-09T11:31:56.907Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-10T06:57:01.716Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

